(function(undefined){
	var html_obj = {};

	//表单操作
	html_obj.form = {};

	//生成option列表
	html_obj.form.create_option = function(obj, title, data, v, t, def,optgroup){
		  str='';
		if(title!=''){
			str = '<option value="">'+title+'</option>';
		}
		typeof optgroup == "undefined" ? optgroup=false : optgroup;
		optgroup_str = '';
		for(i in data){
			if(optgroup && optgroup_str != data[i][optgroup]){
				str += '<optgroup label="'+data[i][optgroup]+'">';
				optgroup_str = data[i][optgroup];
			}
			if(data[i][v] == def){
				str += '<option value="'+data[i][v]+'" selected="selected">'+data[i][t]+'</option>';
			}else{
				str += '<option value="'+data[i][v]+'">'+data[i][t]+'</option>';
			}
			if(optgroup && optgroup_str != data[i][optgroup]){
				str += '</optgroup>';
				optgroup_str = data[i][optgroup];
			}
		}

		obj.html(str);
	}
	//生成hidden 表单，获取城市名称
	html_obj.form.create_hidden = function(obj, title, data, v, t, def,optgroup){
		for(i in data){

			if(data[i][v] == def){

				obj.val(data[i][t]);
				var v = data[i][t];
			}
		}
		return v;
	}


	//复选框表单全选，obj为全选按钮，form为全选表单所在的表单对象，默认为与obj在同一form
	//此函数已经升级，2012-09-19 13:42:37 见 html_obj.form.select_all_init
	html_obj.form.select_all = function(obj, form){
		//找到所在的表单元素
		var form_obj = form == undefined ? $(obj).parents("form") : form;
		//得到复选框列表
		var box_list = form_obj.find("input[type=checkbox]");
		total = 0;
		checked = 0;

		box_list.each(function(k, v){
			var tmp = v.id.indexOf(obj.id+"_")

			if(tmp === 0){
				total ++;
				v.checked = obj.checked;
				if(v.checked){
					checked ++;
				}
			}
		});
		if(typeof select_all_handle == "function"){
			select_all_handle({
				"total_select": total,
				"checked": checked
			});
		}
	}
	//复选框表单全选，为函数 html_obj.form.select_all 的扩展
	html_obj.form.select_all_init = function(param){

		html_obj.form.select_all_config = {
			'form_obj': $("#form1"),
			'prex_id': 'id'
		}

		for(var i in param){
			html_obj.form.select_all_config[i] = param[i];
		}

		html_obj.form.select_all_config.form_obj.find("input[type=checkbox]").each(function(){
			var tmp = this.id.indexOf(html_obj.form.select_all_config.prex_id+"_")

			if(tmp === 0){
				$(this).bind("click", function(){
					var list = html_obj.form.select_all_config.form_obj.find("input[type=checkbox]");
					checked = 0;
					list_total = 0;
					list.each(function(){
						var tmp = this.id.indexOf(html_obj.form.select_all_config.prex_id+"_")
						if(tmp === 0){
							if(this.checked){
								checked ++;
							}
							list_total ++;
						}
					})
					if(checked == list_total){
						$("#"+html_obj.form.select_all_config.prex_id).attr("checked", true);
					}else{
						$("#"+html_obj.form.select_all_config.prex_id).attr("checked", false);
					}

					if(typeof select_all_handle == "function"){
						select_all_handle({
							"total_select": list_total,
							"checked": checked
						});
					}

				})
			}
		})

	}

	html_obj.autoimg = function(){

		$("img[autosize]").each(function(i, n){
			$(this).css("display","block")
			var _this = $(this);
			var size = $(this).attr("autosize").split(",");
			var maxWidth = size[0];
			var maxHeight = size[1];

			var hRatio;
			var wRatio;
			var Ratio = 1;
			var w = _this.width();
			var h = _this.height();
			wRatio = maxWidth / w;
			hRatio = maxHeight / h;
			if (maxWidth ==0 && maxHeight==0){
				Ratio = 1;
			}else if (maxWidth==0){

				if(hRatio<1) Ratio = hRatio;

			}else if (maxHeight==0){

				if (wRatio<1) Ratio = wRatio;

			}else if (wRatio<1 || hRatio<1){

				Ratio = (wRatio<=hRatio?wRatio:hRatio);
			}
			if (Ratio<1){
				w = w * Ratio;
				h = h * Ratio;
			}
		   //	img_height = h;
			//img_width = w;
			_this.height(h);
			_this.width(w);
		});
	}

	html_obj.tabs = function(param){

		var config = {
			frame: $("#tabs"),
			label: "li",
			classname: "hover",
			handle: null,
			firstselect: true
		};

		for(var i in param){
			config[i] = param[i];
		}

		config.frame.find(config.label).each(function(i, n){
			$(this).bind("click", function(){
				config.frame.find(config.label).removeClass(config.classname);
				$(this).addClass(config.classname);
				if(config.handle !== null){
					return config.handle(this);
				}
				return false;
			})
			if(config.firstselect && i==0){
				$(this).trigger("click");
			}
		});

	}



	if(window.html_obj == undefined){
		window.html_obj = html_obj;
	}
})();